/**
 * [一句话描述该类的功能]
 *
 * @author : [kai]
 * @version : [v1.0]
 * @createTime : [2022/10/25 13:59]
 */


package com.find.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.find.controller.utils.R;
import com.find.domain.Forum;
import com.find.domain.Item;
import com.find.domain.User;
import com.find.service.ForumService;
import com.find.service.ItemService;
import com.find.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="帖子相关")
@RequestMapping("/forums")
public class ForumController {

    @Autowired
    private ForumService forumService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItemService itemService;
    @ApiOperation("分页获取帖子")
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize,HttpServletResponse response) {

        IPage<Forum> page = forumService.getPage(currentPage, pageSize);
        if (currentPage > page.getPages()) {
            page = forumService.getPage((int) page.getPages(), pageSize);
        }
        response.setStatus(200);
        return new R(true, page);

    }
    @ApiOperation(value="根据条件筛选全部帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name="user_id",type ="",required = false,value=""),
            @ApiImplicitParam(name="item_id",type ="",required = false,value=""),
            @ApiImplicitParam(name="forum_des",type ="",required = false,value="模糊匹配"),
            @ApiImplicitParam(name="forum_id",type ="",required = false,value=""),
            @ApiImplicitParam(name="forum_type",type ="",required = false,value="帖子类型，0寻找失物，1提供失物，2其他"),
            @ApiImplicitParam(name="forum_time",type ="",required = false,value=""),
            @ApiImplicitParam(name="forum_title",type ="",required = false,value="模糊匹配"),
            @ApiImplicitParam(name="forum_state",type ="",required = false,value="解决状态，默认0未解决1解决"),
    })
    @GetMapping("/select")
    @ApiImplicitParam(name="map",value = "")
    public R getByMap(@RequestParam Map<String, Object> map,HttpServletResponse response) {
        response.setStatus(200);
        return new R(true, forumService.myListByMap(map));
    }
    @ApiOperation("根据条件获取分页帖子，参数同select")
    @GetMapping("{currentPage}/{pageSize}/select")
    public R getPageByMap(@RequestParam Map<String, Object> map, @PathVariable int currentPage, @PathVariable int pageSize, HttpServletResponse response) {
        IPage<Forum> page = forumService.getMapPage(map,currentPage, pageSize);
        if (currentPage > page.getPages()) {
            page = forumService.getMapPage(map,(int) page.getPages(), pageSize);
        }
        response.setStatus(200);
        return new R(true, page);
    }
    @ApiOperation("模糊查询")
    @GetMapping("{currentPage}/{pageSize}/find")
    public R getPageByWord(@PathVariable int currentPage, @PathVariable int pageSize,
                           @RequestParam("word")String word,@RequestParam("forum_type")int forumType,
                           HttpServletResponse response){
        IPage<Forum> page = forumService.getWordPage(word,forumType,currentPage, pageSize);
        if (currentPage > page.getPages()) {
            page = forumService.getWordPage(word,forumType,(int) page.getPages(), pageSize);
        }
        response.setStatus(200);
        return new R(true, page);

    }
    @ApiOperation("根据帖子id获取帖子")
    @GetMapping("/{id}")
    public R getById(@PathVariable int id,  HttpServletResponse response){
        Forum forum=forumService.getById(id);
        Item item=itemService.getById(forum.getItemId());
        User user=userService.getById(forum.getUserId());
        List<Object> list=new ArrayList<>();
        list.add(forum);
        list.add(item);
        list.add(user);
        if(forum==null){
            response.setStatus(500);
            return new R(false,"未找到该帖子");
        }
        response.setStatus(200);
        return new R(true,list);
    }
    @ApiOperation("新建帖子,传入帖子对象")
    @PostMapping
    public R putForum(@RequestBody Forum forum, HttpServletRequest request, HttpServletResponse response){
        response.setStatus(500);
        if(forum.getUserName()==null){
            forum.setUserName(request.getHeader("Cookie"));
        }
        if(forum.getForumDes()==null){
            return new R(false,"帖子内容不能为空");
        }
        if(forum.getForumTitle()==null){
            return new R(false,"帖子标题不能为空");
        }
        if(forum.getItemId()==null){
            return new R(false,"失物id不能为空");
        }
        if(forumService.save(forum)){
            response.setStatus(200);
            return new R(true,"成功发布");
        }
        return new R(false,"发布失败");
    }
    @ApiOperation("根据id删除帖子")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id")int id,HttpServletResponse response){
        if(forumService.removeById(id)){
            response.setStatus(200);
            return new R(true,"成功删除");
        }
        response.setStatus(500);
        return new R(false,"删除失败");
    }
    @ApiOperation("修改帖子，传入帖子对象")
    @PutMapping
    public R updateById(@RequestBody Forum forum,HttpServletResponse response){
        if(forumService.updateById(forum)){
            response.setStatus(200);
            return new R(true,"成功修改");
        }
        response.setStatus(500);
        return new R(false,"修改失败");
    }
}
